app.controller("logCtrl",function($scope,LogService){

    /**
     * 时间格式化
     * @param fmt
     * @returns {*}
     * @constructor
     */
    Date.prototype.Format = function(fmt)
    {
        var o = {
            "M+" : this.getMonth()+1,                 //月份
            "d+" : this.getDate(),                    //日
            "h+" : this.getHours(),                   //小时
            "m+" : this.getMinutes(),                 //分
            "s+" : this.getSeconds()                 //秒
        };
        if(/(y+)/.test(fmt))
            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
        for(var k in o)
            if(new RegExp("("+ k +")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        return fmt;
    }

    var searchDateStart =(new Date()).Format("yyyy-MM-dd")+" 00:00";
    var searchDateEnd=(new Date()).Format("yyyy-MM-dd")+" 23:59";
    var searchLevel = '0';
    var queryKeyword = '';
    var queryOperateName = '';

    //页面时间输入框初始化
    $scope.searchDateStart=(new Date()).Format("yyyy-MM-dd")+" 00:00";
    $scope.searchDateEnd=(new Date()).Format("yyyy-MM-dd")+" 23:59";

    /**
     * 初始化加载数据
     */
    LogService.getLogsByCondition({
        action : 'getLogsByCondition',
        currentPage : 1,
        pageSize : 25,
        dateStart : searchDateStart,
        dateEnd : searchDateEnd,
        searchLevel : '0',
        queryKeyword : '',
        queryOperateName : ''
    },function(data){
        //展示路由数据表格
        if(data.state == 'SUCCESS'){
            $scope.messages = data.data.gwLogVOList;
            pageOperation($scope,data.pageInfo);
        }else{
            bootbox.alert("查询失败(" + data.reason + ")");
        }
    });

    /**
     * 查询按钮事件
     */
    $scope.findByConditions = function() {
         searchLevel = $scope.searchLevel;
         searchDateStart = $scope.searchDateStart;
         searchDateEnd = $scope.searchDateEnd;
         queryKeyword = $scope.queryKeyword;
        queryOperateName = $scope.queryOperateName;
        $scope.pageSize = $scope.selectPageSize
        LogService.getLogsByCondition({
            action: 'getLogsByCondition',
            logLevel : searchLevel,
            dateStart : searchDateStart,
            dateEnd :searchDateEnd,
            queryKeyword : queryKeyword,
            queryOperateName : queryOperateName,
            currentPage : $scope.selPage,
            pageSize : $scope.pageSize
        }, function (data) {
            if(data.state == 'SUCCESS'){
                $scope.messages = data.data.gwLogVOList;
                pageOperation($scope,data.pageInfo);
            }else{
                $scope.messages=null;
                $scope.pageOperation(null);
                bootbox.alert("查询失败(" + data.reason + ")");
            }
        });
    } ;

    /**
     * 分页查询
     */
    $scope.findByCondition = function() {
        $scope.pageSize = $scope.selectPageSize;
        LogService.getLogsByCondition({
            action: 'getLogsByCondition',
            logLevel : searchLevel,
            dateStart : searchDateStart,
            dateEnd :searchDateEnd,
            queryKeyword : queryKeyword,
            queryOperateName : queryOperateName,
            currentPage : $scope.selPage,
            pageSize : $scope.pageSize
        }, function (data) {
            if(data.state == 'SUCCESS'){
                $scope.messages = data.data.gwLogVOList;
                pageOperation($scope,data.pageInfo);
            }else{
                $scope.messages=null;
                $scope.pageOperation(null);
                bootbox.alert("查询失败(" + data.reason + ")");
            }
        });
    };
    //$scope.searchDateStart =new Date();
    /**
     * 时间空间属性设置
     */
    $('#dateStart').datetimepicker({
        language:  'zh-CN',
        todayBtn:  1,
        autoclose: 1,
        //format : "yyyy-mm-dd hh:ii",
        endDate:new Date()
        //initialDate: "1984-1-1"
    }).on("changeDate",function(ev){
        $("#dateEnd").datetimepicker("setStartDate", $("#dateStart").val());
    });;

    $('#dateEnd').datetimepicker({
        language:  'zh-CN',
        todayBtn:  1,
        autoclose: 1,
        endDate:new Date()
    }).on("changeDate", function (ev) {
        $("#dateStart").datetimepicker("setEndDate", $("#dateEnd").val());
    });

    $scope.changColor = function(logLevel){
        if (logLevel == '0') {
            return {
                'background-color':'#7AD57A'
            }
        } else if (logLevel == '1') {
            return {
                'background-color':'#FFFF00'
            }
        } else if (logLevel == '2') {
            return {
                'background-color':'#EE9A00'
            }
        } else {
            return {
                'background-color':'#D9524E'
            }
        }
    }

});


